/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ia_0;
import ia_0.data_model.*;
import ia_0.data_model.character.Character;
import java.awt.Point;
import java.util.HashMap;
/**
 *
 * @author jderenty
 */
public class World {

    public static HashMap<Point,Type> map;
    public static int h;
    public static int w;
    public static HashMap<Element,Point> elements;
    public static HashMap<Element,Point> elementsNouveaux;

    public World(int i, int i0) {
        h=i-2;
        w=i0-2;
        map=new HashMap<Point, Type>();
        elements=new HashMap<Element, Point>();
        elementsNouveaux=new HashMap<Element, Point>();
    }

    public void iterate(){
        for(Element e:this.elements.keySet()){
            e.run();
        }
//        for(Element e:this.elementsNouveaux.keySet()){
//            e.run();
//        }
        elements.putAll(elementsNouveaux);
        elementsNouveaux.clear();
    }

    public static void addElement(Element e,Point p){
        elementsNouveaux.put(e, p);
    }

    public void removeElement(Element e){
        elements.remove(e);
    }


    public static void move(Character c,Point dest){
        int x=elements.get(c).x;
        int y=elements.get(c).y;
        boolean pVoisine=Math.abs(x-dest.x)<2 && Math.abs(y-dest.y)<2;

        if(free(c, dest) && pVoisine){
            elements.put(c, dest);
        } else{
            System.out.println("Téléportation impossible !!!");
        }

    }

    public static boolean free(Character c,Point dest){
        if(dest.x < 0 || dest.y<0){
            return false;
        }
        if (map.get(dest)==null){
            return true;
        } else {
            if(map.get(dest).equals(Type.jungle)){
                return false;
            } else {
                return true;
            }
        }
    }

    public void type(int i, int i0, Type jungle) {
        map.put(new Point(i, i0), jungle);
    }


    public enum Type{
        jungle, plaine, occupee
    }
}
